<template>
<div>
<el-card shadow="never" style="min-height:650px;">
<div v-if="search_visible" id="search" class="search">
	<el-form ref="form" size="small" :model="searchData" inline>
		<el-form-item label="chat_id">
			<select-page :is_clear="is_clear" url="/admin/Message/getChat_id" :selectval.sync="searchData.chat_id"></select-page>
		</el-form-item>
		<el-form-item label="消息内容">
			<el-input id="content" v-model="searchData.content"  style="width:150px;" placeholder="请输入消息内容"></el-input>
		</el-form-item>
		<el-form-item label="role">
			<el-input id="role" v-model="searchData.role"  style="width:150px;" placeholder="请输入role"></el-input>
		</el-form-item>
		<el-form-item label="openai_api_id">
			<el-input id="openai_api_id" v-model="searchData.openai_api_id"  style="width:150px;" placeholder="请输入openai_api_id"></el-input>
		</el-form-item>
		<el-form-item label="finish_reason">
			<el-input id="finish_reason" v-model="searchData.finish_reason"  style="width:150px;" placeholder="请输入finish_reason"></el-input>
		</el-form-item>
		<el-form-item label="status">
			<el-select style="width:150px" v-model="searchData.status" filterable clearable placeholder="请选择">
				<el-option key="0" label="正常" value="1"></el-option>
				<el-option key="1" label="失败" value="0"></el-option>
				<el-option key="2" label="回复中" value="2"></el-option>
			</el-select>
		</el-form-item>
		<Search :search_data.sync="searchData" @refesh_list="index"></Search>
	</el-form>
</div>
<div class="btn-group" style="margin-top:10px;margin-bottom:10px;">
	<div>
		<el-button v-for="item in button_group" v-if="checkPermission(item.access)" :key="item.access"  :disabled="$data[item.disabled]" :type="item.color" size="mini" :icon="item.icon" @click="fn(item.clickname)">
			<span v-if="item.batch" v-text="$data['batchUpdateStatus']?'批量保存':'批量编辑'"></span>
			<span v-else v-text="item.name"></span>
		</el-button>
	</div>
	<div><TableTool :search_visible.sync="search_visible"   @refesh_list="index"></TableTool></div>
</div>
<el-table :row-class-name="rowClass" @selection-change="selection"  @row-click="handleRowClick"  row-key="message_id"  :header-cell-style="{ background: '#eef1f6', color: '#606266' }" @sort-change='sortChange' v-loading="loading"  ref="multipleTable" border class="eltable" :data="list"  style="width: 100%">
	<el-table-column align="center" type="selection" width="42"></el-table-column>
	<el-table-column align="center" type = '' property="message_id"  label="编号"  width="70">
	</el-table-column>
	<el-table-column align="center"  property="chat_id"  label="chat_id" show-overflow-tooltip width="">
	</el-table-column>
	<el-table-column align="left"  property="content"  label="消息内容"  width="500">
	</el-table-column>
	<el-table-column align="center"  property="role"  label="role" show-overflow-tooltip width="">
	</el-table-column>
	<el-table-column align="center"  property="openai_api_id"  label="openai_api_id" show-overflow-tooltip width="">
	</el-table-column>
	<el-table-column align="center"  property="finish_reason"  label="finish_reason" show-overflow-tooltip width="">
	</el-table-column>
	<el-table-column align="center"  property="create_time"  label="创建时间" show-overflow-tooltip width="">
		<template slot-scope="scope">
			{{parseTime(scope.row.create_time)}}
		</template>
	</el-table-column>
	<el-table-column align="center"  property="status"  label="status" show-overflow-tooltip width="">
		<template slot-scope="scope">
			<el-tag type="primary" v-if="scope.row.status == '1'" size="mini" effect="dark">正常</el-tag>
			<el-tag type="danger" v-if="scope.row.status == '0'" size="mini" effect="dark">失败</el-tag>
			<span v-if="scope.row.status == '2'" >回复中</span>
		</template>
	</el-table-column>
	<el-table-column :fixed="$store.getters.device == 'mobile'?false:'right'" label="操作" align="center" width="190">
		<template slot-scope="scope">
			<div v-if="scope.row.message_id">
				<el-button  size="mini" v-if="checkPermission('/admin/Message/update')" icon="el-icon-edit" type="primary" @click="update(scope.row)" >修改</el-button>
				<el-button  size="mini" v-if="checkPermission('/admin/Message/delete')" icon="el-icon-delete" type="danger" @click="del(scope.row)" >删除</el-button>
			</div>
		</template>
	</el-table-column>
</el-table>
<Page :total="page_data.total" :page.sync="page_data.page" :limit.sync="page_data.limit" @pagination="index" />
</el-card>

<!--添加-->
<Add :show.sync="dialog.addDialogStatus" size="small" @refesh_list="index"></Add>
<!--修改-->
<Update :info="updateInfo" :show.sync="dialog.updateDialogStatus" size="small" @refesh_list="index"></Update>
<!--查看详情-->
<Detail :info="detailInfo" :show.sync="dialog.detailDialogStatus" size="small" @refesh_list="index"></Detail>

</div>
</template>

<script>
import Search from '@/components/common/Search.vue'
import Page from '@/components/common/Page.vue'
import TableTool from '@/components/common/TableTool.vue'
import SelectPage from '@/components/common/SelectPage.vue'
import Add from '@/views/admin/message/add.vue'
import Update from '@/views/admin/message/update.vue'
import Detail from '@/views/admin/message/detail.vue'
export default {
	components: {
		Search,
		Page,
		TableTool,
		SelectPage,
		Add,
		Update,
		Detail,
	},
	data: function() {
		return {
			dialog: {
				addDialogStatus : false,
				updateDialogStatus : false,
				detailDialogStatus : false,
			},
			searchData:{},
			button_group:[
				// {name:'添加',color:'success',access:'/admin/Message/add',icon:'el-icon-plus',disabled:'',clickname:'add'},
				// {name:'修改',color:'primary',access:'/admin/Message/update',icon:'el-icon-edit',disabled:'single',clickname:'update'},
				// {name:'删除',color:'danger',access:'/admin/Message/delete',icon:'el-icon-delete',disabled:'multiple',clickname:'del'},
				// {name:'查看详情',color:'info',access:'/admin/Message/detail',icon:'el-icon-view',disabled:'single',clickname:'detail'},
			],
			loading: false,
			page_data: {
				limit: 20,
				page: 1,
				total:20,
			},
			order:'',
			sort:'',
			ids: [],
			single:true,
			multiple:true,
			search_visible:true,
			list: [],
			updateInfo:{},
			detailInfo:{},
			is_clear:false,
		}
	},
	methods:{
		index(){
			let param = {limit:this.page_data.limit,page:this.page_data.page,order:this.order,sort:this.sort}
			Object.assign(param, this.searchData,this.$route.query)
			this.loading = true
			this.$axios.post('/admin/Message/index',param).then(res => {
				if(res.data.status == 200){
					this.list = res.data.data.data
					this.page_data.total = res.data.data.total
					this.loading = false
				}else{
					this.$message.error(res.data.msg);
				}
			})
		},
		updateExt(row,field){
			if(row.message_id){
				this.$axios.post('/admin/Message/updateExt',{message_id:row.message_id,[field]:row[field]}).then(res => {
					if(res.data.status == 200){
						this.$message({message: res.data.msg, type: 'success'})
					}else{
						this.$message.error(res.data.msg)
					}
				})
			}
		},
		add(){
			this.dialog.addDialogStatus = true
		},
		update(row){
			let id = row.message_id ? row.message_id : this.ids.join(',')
			this.$axios.post('/admin/Message/getUpdateInfo',{message_id:id}).then(res => {
				if(res.data.status == 200){
					this.dialog.updateDialogStatus = true
					this.updateInfo = res.data.data
				}else{
					this.$message.error(res.data.msg)
				}
			})
		},
		del(row){
			this.$confirm('确定操作吗?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				let ids = row.message_id ? row.message_id : this.ids.join(',')
				this.$axios.post('/admin/Message/delete',{message_id:ids}).then(res => {
					if(res.data.status == 200){
						this.$message({message: res.data.msg, type: 'success'})
						this.index()
					}else{
						this.$message.error(res.data.msg)
					}
				})
			}).catch(() => {})
		},
		detail(row){
			this.dialog.detailDialogStatus = true
			this.detailInfo = {message_id:row.message_id ? row.message_id : this.ids.join(',')}
		},
		selection(selection) {
			this.ids = selection.map(item => item.message_id)
			this.single = selection.length != 1
			this.multiple = !selection.length
		},
		handleRowClick(row, rowIndex,event){
			if(event.target.className !== 'el-input__inner'){
				this.$refs.multipleTable.toggleRowSelection(row)
			}
		},
		rowClass ({ row, rowIndex }) {
			for(let i=0;i<this.ids.length;i++) {
				if (row.message_id === this.ids[i]) {
					return 'rowLight'
				}
			}
		},
		sortChange(val){
			if(val.order == 'descending'){
				this.order= 'desc'
			}
			if(val.order == 'ascending'){
				this.order= 'asc'
			}
			this.sort = val.prop
			this.index()
		},
		fn(method){
			this[method](this.ids)
		},
	},
	mounted(){
		this.index()
	},
}
</script>

<style  lang="scss">
@import '@/assets/scss/common.scss'
</style>
